// RecordsPage.tsx
import React from 'react';
import { Link } from 'react-router-dom';
import { ArrowLeft } from 'lucide-react';
import { RecordsFilter } from '../components/Records/RecordsFilter';
import { RecordsTable } from '../components/Records/RecordsTable';
import { useRecords } from '../hooks/useRecords';
import { Loading } from '../components/common/loading';
import { Toast } from '../components/common/Toast';
import { useToast } from '../hooks/useToast';
import { Sidebar } from '../components/layout/Sidebar';

export function RecordsPage() {
  const { toast, showToast, hideToast } = useToast();
  const {
    records,
    isLoading,
    error,
    handleFilterChange
  } = useRecords();

  React.useEffect(() => {
    if (error) {
      showToast('获取记录失败，请稍后重试');
    }
  }, [error, showToast]);

  return (
    <div className="min-h-screen bg-gray-100">
      <Sidebar/>
      <div className="max-w-7xl ml-64 p-6">
        <div className="flex items-center gap-4 mb-6">
          <Link to="/" className="p-2 hover:bg-gray-200 rounded-full">
            <ArrowLeft size={24} />
          </Link>
          <h1 className="text-3xl font-bold">配送记录查询</h1>
        </div>

        <div className="space-y-6">
          <RecordsFilter onFilterChange={handleFilterChange} />
          {isLoading ? (
            <Loading />
          ) : records.length === 0 ? (
            <div className="text-center text-gray-500 py-8">
              暂无配送记录
            </div>
          ) : (
            <RecordsTable records={records} />
          )}
        </div>
      </div>
      {toast && (
        <Toast
          message={toast.message}
          type={toast.type}
          onClose={hideToast}
        />
      )}
    </div>
  );
}
